package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.BsDishDao;
import io.renren.modules.app.dao.BsEmployeeDao;
import io.renren.modules.app.dao.BsMemberDao;
import io.renren.modules.app.dao.BsOrderdishDao;
import io.renren.modules.app.entity.*;
import io.renren.modules.app.service.BsOrderdishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Service("bsOrderdishService")
@Slf4j
public class BsOrderdishServiceImpl extends ServiceImpl<BsOrderdishDao, BsOrderdishEntity> implements BsOrderdishService {

    @Resource
    private BsOrderdishDao bsOrderdishDao ;

    @Resource
    private BsDishDao bsDishDao ;

    @Resource
    private BsMemberDao bsMemberDao ;

    @Resource
    private BsEmployeeDao bsEmployeeDao ;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<BsOrderdishEntity> wrapper = new LambdaQueryWrapper<>();

        if (params.get("memberName") != null && !params.get("memberName").equals("")){
            LambdaQueryWrapper<BsMemberEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(BsMemberEntity::getMemberName,params.get("memberName"));
            List<BsMemberEntity> bsMemberEntities = bsMemberDao.selectList(queryWrapper);
            List<String> list = new ArrayList<>();
            if (bsMemberEntities.size() > 0){
                for (BsMemberEntity bsMemberEntity : bsMemberEntities) {
                    list.add(bsMemberEntity.getMemberId());
                }
                wrapper.in(BsOrderdishEntity::getMemberId,list);
            }else {
                IPage<BsOrderdishEntity> page = this.page(
                        new Query<BsOrderdishEntity>().getPage(params),
                        new LambdaQueryWrapper<BsOrderdishEntity>().eq(BsOrderdishEntity::getId,-1)
                );
                return new PageUtils(page);
            }
        }
        if (params.get("orderdishEmployeeName") != null && !params.get("orderdishEmployeeName").equals("")){
            List<BsEmployeeEntity> entities = bsEmployeeDao.selectList(
                    new LambdaQueryWrapper<BsEmployeeEntity>()
                            .like(BsEmployeeEntity::getEmployeeName, params.get("orderdishEmployeeName")));
            List<String> list = new ArrayList<>();
            if (entities.size() > 0){
                for (BsEmployeeEntity entity : entities) {
                    list.add(entity.getEmployeeId());
                }
                wrapper.in(BsOrderdishEntity::getOrderdishEmployeeid,list);
            }else {
                IPage<BsOrderdishEntity> page = this.page(
                        new Query<BsOrderdishEntity>().getPage(params),
                        new LambdaQueryWrapper<BsOrderdishEntity>().eq(BsOrderdishEntity::getId,-1)
                );
                return new PageUtils(page);
            }

        }
        IPage<BsOrderdishEntity> page = this.page(
                new Query<BsOrderdishEntity>().getPage(params),
                wrapper
        );
        for (BsOrderdishEntity record : page.getRecords()) {
            //会员姓名数据清洗
            List<BsMemberEntity> entities = bsMemberDao.selectList(
                    new LambdaQueryWrapper<BsMemberEntity>()
                            .eq(BsMemberEntity::getMemberId, record.getMemberId()));
            record.setMemberName(entities.get(0).getMemberName());
            //送餐人员姓名清洗
            List<BsEmployeeEntity> entities1 = bsEmployeeDao.selectList(
                    new LambdaQueryWrapper<BsEmployeeEntity>()
                            .eq(BsEmployeeEntity::getEmployeeId, record.getOrderdishEmployeeid()));
            record.setOrderdishEmployeeName(entities1.get(0).getEmployeeName());
            //菜品名称数据清洗
            List<BsDishEntity> entities2 = bsDishDao.selectList(
                    new LambdaQueryWrapper<BsDishEntity>()
                            .eq(BsDishEntity::getId, record.getOrderdishId()));
            record.setOrderdishName(entities2.get(0).getDishName());

        }

        return new PageUtils(page);
    }

    @Override
    public List<BsDishEntityVo> getDish() {
        List<BsDishEntity> entities = bsDishDao.selectList(null);
        List<BsDishEntityVo> list = new ArrayList<>();
        for (BsDishEntity entity : entities) {
            BsDishEntityVo bsDishEntityVo = new BsDishEntityVo();
            bsDishEntityVo.setOrderdishId(entity.getId());
            bsDishEntityVo.setOrderdishName(entity.getDishName());
            list.add(bsDishEntityVo);
        }
        return list ;
    }

    @Override
    public void saveOrderDish(BsOrderdishEntity bsOrderdish) {
        try {
            //会员编号存在性校验
            String memberId = bsOrderdish.getMemberId();
            LambdaQueryWrapper<BsMemberEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BsMemberEntity::getMemberId,memberId);
            List<BsMemberEntity> entityList = bsMemberDao.selectList(queryWrapper);
            if (entityList.size() < 1){
                throw new RRException("请校验会员编号");
            }
            //送餐人员存在性校验
            if (StringUtils.isNotBlank(bsOrderdish.getOrderdishEmployeeid())){
                String orderdishEmployeeid = bsOrderdish.getOrderdishEmployeeid();//送餐人员编号
                LambdaQueryWrapper<BsEmployeeEntity> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(BsEmployeeEntity::getEmployeeId,orderdishEmployeeid);
                List<BsEmployeeEntity> list = bsEmployeeDao.selectList(wrapper);
                if (list.size() < 1){
                    throw new RRException("请校验送餐人员编号");
                }
            }else {
                bsOrderdish.setOrderdishEmployeeid("");
            }
            bsOrderdishDao.insert(bsOrderdish);
        }catch (Exception e){
            log.info("报错类及所在方法：{}","BsOrderdishService --> saveOrderDish");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    @Override
    public void updateOrderDish(BsOrderdishEntity bsOrderdish) {
        try {
            //该修改功能限制修改会员编号及送餐人员编号
            String memberId = bsOrderdish.getMemberId();//会员编号
            String orderdishEmployeeid = bsOrderdish.getOrderdishEmployeeid();//送餐人员编号
            BsOrderdishEntity entity = bsOrderdishDao.selectById(bsOrderdish.getId());
            if (!memberId.equals(entity.getMemberId())){
                throw new RRException("会员编号不可修改");
            }
            if (StringUtils.isNotBlank(orderdishEmployeeid)){
                if (!orderdishEmployeeid.equals(entity.getOrderdishEmployeeid())){
                    throw new RRException("送餐人员编号不可修改");
                }
            }
            bsOrderdishDao.updateById(bsOrderdish);
        }catch (Exception e){
            log.info("报错类及所在方法：{}","BsOrderdishService --> updateOrderDish");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

}
